package com.shiwaixiangcun.customer.ui.activity

import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.text.TextUtils
import android.util.Log
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.google.gson.reflect.TypeToken
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.shiwaixiangcun.customer.Common
import com.shiwaixiangcun.customer.GlobalApi
import com.shiwaixiangcun.customer.GlobalConfig
import com.shiwaixiangcun.customer.R
import com.shiwaixiangcun.customer.adapter.AdapterAllTools
import com.shiwaixiangcun.customer.base.BaseActivity
import com.shiwaixiangcun.customer.entity.ResponseEntity
import com.shiwaixiangcun.customer.entity.ToolCategoryBean
import com.shiwaixiangcun.customer.utils.*
import kotlinx.android.synthetic.main.activity_category.*
import kotlinx.android.synthetic.main.top_bar_white.*
import java.util.*

/**
 * 全部服务Activity
 *
 * @author Administrator
 */
class AllToolsActivity : BaseActivity(), View.OnClickListener {


    private var mList: MutableList<AdapterAllTools.MySection>? = null
    private var mAdapterService: AdapterAllTools? = null
    private var isLogin = ""
    private var siteID: Int = 0

    private var mJsonList: List<String>? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_more_tools)
        initViewAndEvent()
        initData()
    }

    private fun initData() {
        OkGo.get<String>(GlobalApi.getToolCategory)
                .params("siteId", siteID)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>) {
                        val type = object : TypeToken<ResponseEntity<ToolCategoryBean>>() {

                        }.type
                        val responseEntity = JsonUtil.fromJson<ResponseEntity<ToolCategoryBean>>(response.body(), type) ?: return
                        when (responseEntity.responseCode) {
                            1001 -> {

                                val titleList = responseEntity.data.children
                                mList!!.clear()
                                for (fatherItem in titleList) {

                                    val typeJson = JsonUtil.toJson(fatherItem)
                                    val title = AdapterAllTools.MySection(true, fatherItem.name, fatherItem.sign, typeJson)
                                    mList!!.add(title)
                                    val childrenList = fatherItem.children
                                    for (childrenItem in childrenList) {

                                        val treeBean = ToolCategoryBean.ChildrenBeanX.ChildrenBean()
                                        treeBean.name = childrenItem.name
                                        treeBean.appCategoryStatus = childrenItem.appCategoryStatus
                                        treeBean.link = childrenItem.link
                                        treeBean.imageLink = childrenItem.imageLink
                                        treeBean.sign = childrenItem.sign
                                        treeBean.isAuthorization = childrenItem.isAuthorization
                                        val childItem = AdapterAllTools.MySection(treeBean, fatherItem.sign, typeJson)
                                        mList!!.add(childItem)
                                    }
                                }


                                mAdapterService!!.notifyDataSetChanged()
                            }
                            else -> {
                            }
                        }


                    }
                })
    }


    private fun initViewAndEvent() {

        siteID = AppSharePreferenceMgr.get(mContext, GlobalConfig.CURRENT_SITE_ID, 20) as Int
        tv_page_name!!.text = "全部服务"
        isLogin = SharePreference.getStringSpParams(mContext, Common.ISORNOLOGIN, Common.SIORNOLOGIN)
        mList = ArrayList()
        mJsonList = ArrayList()
        back_left!!.setOnClickListener(this)
        mAdapterService = AdapterAllTools(mList)
        rv_category!!.layoutManager = GridLayoutManager(this, 2)
        rv_category!!.adapter = mAdapterService
        mAdapterService!!.openLoadAnimation(BaseQuickAdapter.ALPHAIN)
        mAdapterService!!.onItemClickListener = BaseQuickAdapter.OnItemClickListener { adapter, view, position ->
            Log.e(BUG_TAG, "click")
            val bean = adapter.data[position] as AdapterAllTools.MySection
            val treeBean = bean.treeBean ?: return@OnItemClickListener

            if (treeBean.isAuthorization) {
                if (StringUtil.isEmpty(isLogin)) {
                    readyGo(LoginActivity::class.java)
                } else {
                    if (treeBean.appCategoryStatus == "H5") {
                        val bundle = Bundle()
                        bundle.putString("h5_url", treeBean.link)
                        bundle.putString("h5_name", treeBean.name)
                        if (!TextUtils.isEmpty(treeBean.link)) {
                            readyGo(CommonWebActivity::class.java, bundle)
                        }
                    } else {
                        UIClickManager.toolClick(mContext, treeBean.sign)
                    }

                }
            } else {
                if (treeBean.appCategoryStatus == "H5") {
                    val bundle = Bundle()
                    bundle.putString("h5_url", treeBean.link)
                    bundle.putString("h5_name", treeBean.name)

                    if (!TextUtils.isEmpty(treeBean.link)) {
                        readyGo(CommonWebActivity::class.java, bundle)
                    }
                } else {
                    UIClickManager.toolClick(mContext, treeBean.sign)
                }

            }


            //                if (treeBean.isAuthorization()) {
            //                            if (StringUtil.isEmpty(isLogin)) {
            //                                toolClick(LoginActivity.class);
            //                            } else {
            //
            //                            }
            //                        } else {
            //                    UIClickManager.INSTANCE.toolClick(mContext, treeBean.getSign());
            //
            //                        }
            //
            //
        }


    }

    override fun onResume() {
        super.onResume()
        isLogin = SharePreference.getStringSpParams(mContext, Common.ISORNOLOGIN, Common.SIORNOLOGIN)
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.back_left -> finish()
            else -> {
            }
        }
    }
}
